package main

import (
	"fmt"
	"time"
	"math/rand"
)

/**
 * @description 
 * @time 2019/2/23 22:41
 * @version 
 */

func generator() chan int {
	out := make(chan int)
	go func() {
		i := 0
		for {
			time.Sleep(time.Duration(rand.Intn(1500)) * time.Millisecond)
			out <- i
			i ++
		}
	}()
	return out
}

func worker(id int, c chan int) {
	for n := range c {
		fmt.Printf("worker %d received %c\n", id, n)
	}
}

func createWorker(id int) chan<- int {
	c := make(chan int)
	go worker(id, c)
	return c
}

func main() {
	var c1, c2 = generator(),generator()
	worker := createWorker(0)
	for  {
		select {
		case n := <-c1:
			worker <- n
		case n := <-c2:
			worker <- n
		}
	}

}
